﻿@using kangsuce.Model
@{
    ViewBag.Title = "菜单权限设置";
}
@section Css{
    <style type="text/css">
        
    </style>
}
<div class="wrapper wrapper-content animated fadeInUp">
    
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>权限分配 <small></small></h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal">
                        @foreach (KSC_Function item in ViewBag.List)
                        {
                            <div class="form-group">
                                <label class="checkbox-inline i-checks">
                                    <input type="checkbox" id="checkbox-f-@item.functionId" data-m="m-@item.functionId" class="function">@item.functionName
                                </label>
                                <div class="col-sm-12" style="padding-left: 20px">
                                    @foreach (KSC_Menu menu in item.menuList)
                                    {
                                        <label class="checkbox-inline i-checks">
                                            <input type="checkbox" id="checkbox-m-@menu.menuId" class="menu m-@item.functionId" data-f="@item.functionId" value="@menu.menuId">@menu.menuName
                                        </label>
                                    }
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        }
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" id="btn_Submit" type="button">保存内容</button>
                                <button class="btn btn-white" id="btn_Back" type="button">取消</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


@section Js{
    <script type="text/javascript">
        $(function () {
            var urlParms = getRequest();
            $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green" });
            $.getJSON("/Menu/GetMenuRoleList?roleId=" + urlParms.roleId, function (json) {
                if (json.result == "success") {
                    $.each(json.data, function(index, item) {
                        debugger;
                        if (item.isChecked == "1") {
                            $("#checkbox-m-" + item.menuId).iCheck('check');
                            if ($(".m-" + item.functionId + ":checked").length == $(".m-" + item.functionId).length) {
                                $("#checkbox-f-" + item.functionId).iCheck("check");
                            }
                        }
                    });

                }
            });
            $("small").text("（" + decodeURIComponent(urlParms.roleName) + "）");
            $(".function").on('ifClicked', function () {
                $(this).iCheck('toggle');
                var m = $(this).attr("data-m");
                if ($(this).is(':checked')) {
                    $("." + m).iCheck("check");
                } else {
                    $("." + m).iCheck("uncheck");
                }

            });

            $(".menu").on('ifClicked', function () {
                $(this).iCheck('toggle');
                var fId = $(this).attr("data-f");
                if ($(this).is(':checked')) {
                    if ($(".m-" + fId + ":checked").length == $(".m-" + fId).length) {
                        $("#checkbox-f-" + fId).iCheck("check");
                    }
                } else {
                    $("#checkbox-f-" + fId).iCheck("uncheck");
                }

            });

            $("#btn_Submit").click(function() {

                if ($(".menu:checked").length == 0) {
                    layer.msg("至少选择一个菜单", { time: 2000 });
                    return;
                }
                var str = '';
                $.each($(".menu:checked"), function(index, item) {
                    var fId = $(item).attr("data-f");
                    var mId = $(item).val();
                    str += '|' + fId + ',' + mId;
                });
                str = str.slice(1);
                $.post("/Menu/MenuRoleSave", { ids: str, roleId: $.getUrlParam("roleId") }, function(json) {
                    if (json.result == "success") {
                        var backUrl = $.getUrlParam("backUrl");
                        layer.msg("保存成功", { icon: 1, time: 2000 }, function() {
                            location.href = backUrl;
                        });
                    } else {
                        layer.msg(json.msg, { icon: 5, time: 2000 });
                    }
                }, 'json');
            });

            $("#btn_Back").click(function() {
                location.href = $.getUrlParam("backUrl");
            });
        });

        function getRequest() {

            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
    </script>
}